package com.su.sweet.controller;


import com.su.sweet.model.SMenu;
import com.su.sweet.service.ISMenuService;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Result;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author su
 * @since 2025-01-02
 */
@Slf4j
@Controller
@Mapping("/menu")
public class SMenuController {
    @Inject
    private ISMenuService menuService;

    @Get
    @Mapping("/all")
    public Result all() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("id", 2);
        map.put("parentId", null);
        map.put("title", "分析页");
        map.put("icon", "DashboardOutlined");
        map.put("component", "/dashboard/analysis");
        map.put("keepAlive", true);
        map.put("path", "/dashboard");
        map.put("name", "DashboardAnalysis");
        map.put("locale", "menu.dashboard.analysis");
        list.add(map);
        map = new HashMap<>();
        map.put("id", 26);
        map.put("parentId", null);
        map.put("title", "权限管理");
        map.put("icon", "TableOutlined");
        map.put("component", "RouteView");
        map.put("path", "/list");
        map.put("name", "List");
        list.add(map);
        map = new HashMap<>();
        map.put("id", 41);
        map.put("parentId", 26);
        map.put("title", "用户管理");
        map.put("component", "/list/table-list");
        map.put("path", "/list/table-list");
        map.put("name", "ConsultTable");
        list.add(map);
        map = new HashMap<>();
        map.put("id", 42);
        map.put("parentId", 26);
        map.put("title", "角色管理");
        map.put("component", "/role/role-list");
        map.put("path", "/role/role-list");
        map.put("name", "RoleTable");
        list.add(map);
        map = new HashMap<>();
        map.put("id", 43);
        map.put("parentId", 26);
        map.put("title", "菜单管理");
        map.put("component", "/menu/menu1");
        map.put("path", "/menu/menu1");
        map.put("name", "MenuManagement");
        list.add(map);
        map = new HashMap<>();
        map.put("id", 27);
        map.put("parentId", null);
        map.put("title", "系统管理");
        map.put("icon", "SettingOutlined");
        map.put("component", "RouteView");
        map.put("path", "/system");
        map.put("name", "System");
        list.add(map);
        map = new HashMap<>();
        map.put("id", 44);
        map.put("parentId", 27);
        map.put("title", "日志管理");
        map.put("component", "/log/log-list");
        map.put("path", "/log/log-list");
        map.put("name", "LogTable");
        list.add(map);
        return Result.succeed(list);
    }

    /**
     * 新增菜单
     */
    @Post
    @Mapping("/add")
    public Result add(SMenu menu) {
        boolean success = menuService.saveMenu(menu);
        return success ? Result.succeed() : Result.failure("新增菜单失败");
    }

    /**
     * 删除菜单
     */
    @Delete
    @Mapping("/{id}")
    public Result delete(@Path Long id) {
        boolean success = menuService.deleteMenu(id);
        return success ? Result.succeed() : Result.failure("删除菜单失败");
    }

    /**
     * 更新菜单
     */
    @Put
    @Mapping("/update")
    public Result update(SMenu menu) {
        boolean success = menuService.updateMenu(menu);
        return success ? Result.succeed() : Result.failure("更新菜单失败");
    }
    /**
     * 获取树形菜单列表
     * @param menuName 菜单名称(模糊查询)
     * @param menuCode 菜单编码(模糊查询)
     * @return 树形菜单列表
     */
    @Mapping("/tree")
    @Post
    public Result getMenuTree(@Param("menuName") String menuName, @Param("menuCode") String menuCode) {
        List<SMenu> menuTree = menuService.getMenuTree(menuName, menuCode);
        Map<String, Object> map = new HashMap<>();
        map.put("list", menuTree);
        return Result.succeed(map);
    }

    /**
     * 获取菜单详情
     */
    @Get
    @Mapping("/detail/{id}")
    public Result detail(@Path Long id) {
        SMenu menu = menuService.getMenuById(id);
        return Result.succeed(menu);
    }
}
